package extract;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.*;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.*;
import java.io.*;

import org.omg.Messaging.SYNC_WITH_TRANSPORT;

public class Jinni {
	public List<String>stars =new LinkedList<String>();
	public List<String>directors=new LinkedList<String>();
	public List<String>writers=new LinkedList<String>();
	public List<String>mood=new LinkedList<String>();
	public List<String>plot=new LinkedList<String>();
	public List<String>genres=new LinkedList<String>();
	public List<String>audience=new LinkedList<String>();
	public List<String> similars=new LinkedList<String>();
	public List<String> rating=new LinkedList<String>();


	String[] attributes={"Stars","Directors","Writers","Rating","Mood","Plot","Genres","Audience","Similars"};
	String content;

	String input_file_address="names.txt";
	public List<String> movieNames=new LinkedList<String>();
	public List<String> not_found_names=new LinkedList<String>();
	
	public FileWriter fw;
	public BufferedWriter bw;
	
	double count=0;

	public void read()
	{
		try {
			FileReader reader=new FileReader(input_file_address);
			BufferedReader br=new BufferedReader(reader);
			String line="";
			while(br.ready()){
				line+=br.readLine();
			}
			br.close();
			reader.close();
			for(String s:line.split(";")){
				movieNames.add(s);
			}


		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void read2()
	{
		try {
			FileReader reader=new FileReader("dataset.txt");
			BufferedReader br=new BufferedReader(reader);
			String line="";
			while(br.ready()){
				line+=br.readLine()+"\n";
			}
			br.close();
			reader.close();
			Pattern p=Pattern.compile(RegEx.moreSimilars,Pattern.DOTALL);
			Matcher m=p.matcher(line);
			while(m.find()){
				for(String s : m.group(1).split(","))
				{
					movieNames.add(s);
				}
				
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

	public void run()
	{
		try {
			fw=new FileWriter("dataset2.txt");
			bw=new BufferedWriter(fw);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		for(String s:movieNames){
			String url="http://www.jinni.com/movies/"+s;
			
			try
			{
				URL movie = new URL(url);
				URLConnection yc = movie.openConnection();
				BufferedReader in = new BufferedReader(
						new InputStreamReader(
								yc.getInputStream()));
				String inputLine;
				content="";
				while ((inputLine =in.readLine()) != null) 
					content+=inputLine;
				in.close();

				analysis(s);

			}catch(Exception e){
				e.printStackTrace();
				not_found_names.add(s);
			}
			finally
			{
				count++;
				System.out.println("Now we are "+100.00*count/movieNames.size()+"%\n");
			}
		}

		try {
			bw.close();
			fw.close();
			
			System.out.print("not found:");
			for(String s:not_found_names){
				System.out.print(s+",");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	private void analysis(String movieName) throws Exception {
		Class cls=this.getClass();
		bw.write("Movie Name: "+movieName+"\n");
		for(String s:attributes){
			Field fld=cls.getField(s.toLowerCase());
			List<String> list=(List<String>)fld.get(this);
			list.clear();	//make sure the list is empty for each movies
			Method method=cls.getMethod("set"+s, null);
			method.invoke(this, null);

			String line=s+":";
			for(String str:list){
				line+=str+",";
			}
			line+="\n";
			//System.out.println(line);
			
			bw.write(line);
			
		}
		bw.write("\n");
	}

	public  void setStars(){
		Pattern p=Pattern.compile(RegEx.stars,Pattern.DOTALL);
		Matcher m=p.matcher(content);
		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find()){
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find()){
				for(int i=1;i<=m2.groupCount();i++){
					stars.add(m2.group(i));
				}
			}
		}
	}

	public  void setDirectors(){
		Pattern p=Pattern.compile(RegEx.directors,Pattern.DOTALL);
		Matcher m=p.matcher(content);
		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find()){
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find()){
				for(int i=1;i<=m2.groupCount();i++){
					directors.add(m2.group(i));
				}
			}
		}
	}

	public void setWriters(){
		Pattern p=Pattern.compile(RegEx.writers,Pattern.DOTALL);
		Matcher m=p.matcher(content);
		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find()){
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find()){
				for(int i=1;i<=m2.groupCount();i++){
					writers.add(m2.group(i));
				}
			}
		}
	}

	public void setRating(){
		Pattern p=Pattern.compile(RegEx.rating, Pattern.DOTALL);
		Matcher m=p.matcher(content);

		while(m.find())
		{
			//System.out.println(m.group(1));
			rating.add(m.group(1));
		}
	}

	public void setMood(){
		Pattern p=Pattern.compile(RegEx.mood,Pattern.DOTALL);
		Matcher m=p.matcher(content);

		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find())
		{
			//System.out.println(m.group(1));
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++){
					//System.out.println(m2.group(i));
					mood.add(m2.group(i));
				}
			}

		}
	}

	public void setPlot(){
		Pattern p=Pattern.compile(RegEx.plot1,Pattern.DOTALL);
		Matcher m=p.matcher(content);

		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find())
		{
			//System.out.println(m.group(1));
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++){
					//System.out.println(m2.group(i));
					plot.add(m2.group(i));
				}
			}

		}

		p=Pattern.compile(RegEx.plot2,Pattern.DOTALL);
		m=p.matcher(content);


		while(m.find()){
			//System.out.println(m.group());
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++){
					//System.out.println(m2.group(i));
					plot.add(m2.group(i));
				}
			}
		}
	}

	public void setGenres(){
		Pattern p=Pattern.compile(RegEx.genre,Pattern.DOTALL);
		Matcher m=p.matcher(content);

		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find())
		{
			//System.out.println(m.group(1));
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++){
					//System.out.println(m2.group(i));
					genres.add(m2.group(i));
				}
			}

		}
	}

	public void setAudience(){
		Pattern p=Pattern.compile(RegEx.audience,Pattern.DOTALL);
		Matcher m=p.matcher(content);

		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find())
		{
			//System.out.println(m.group(1));
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++){
					//System.out.println(m2.group(i));
					audience.add(m2.group(i));
				}
			}

		}
	}

	public void setSimilars(){
		Pattern p=Pattern.compile(RegEx.similars,Pattern.DOTALL);
		Matcher m=p.matcher(content);

		while(m.find()){
			for(int i=1;i<=m.groupCount();i++){
				//System.out.println(m.group(i));
				similars.add(m.group(i));
			}
		}
	}


}
